package com.travel.server.manager.impl.dal.data;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dao.UserOrderTargetDao;
import com.travel.server.manager.model.UserOrderTarget;
import com.travel.server.manager.model.UserOrderTargetExample;
import com.travel.server.manager.model.dto.data.TargetStatisticsDto;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\12\8 0008 17:55
 * @Desc:
 */
@Component
public class UserTargetDal {

    @Autowired
    private UserOrderTargetDao userOrderTargetDao;

    public void insertOrUpdate(UserOrderTarget userOrderTarget){
        if(userOrderTarget.getId() == null){
            userOrderTargetDao.insertSelective(userOrderTarget);
        }else{
            userOrderTargetDao.updateByPrimaryKey(userOrderTarget);
        }
    }

    public UserOrderTarget selectTarget(Integer userId, Date date, Integer dateType){
        TargetParam param = new TargetParam();
        param.setUserId(userId);
        param.setDate(date);
        param.setType(dateType);
        UserOrderTargetExample example = makeExample(param);
        List<UserOrderTarget> list = userOrderTargetDao.selectByExample(example);
        return CollectionUtil.getFirst(list);
    }

    public Integer selectSumTarget(Integer userId, Date startDate, Date endDate, Integer dateType){
        TargetParam param = new TargetParam();
        param.setUserId(userId);
        param.setStartDate(startDate);
        param.setEndDate(endDate);
        param.setType(dateType);
        UserOrderTargetExample example = makeExample(param);
        return userOrderTargetDao.selectSumByExample(example);
    }

    public List<UserOrderTarget> selectTargetList(Integer userId, Date startDate, Date endDate, Integer dateType){
        TargetParam param = new TargetParam();
        param.setUserId(userId);
        param.setStartDate(startDate);
        param.setEndDate(endDate);
        param.setType(dateType);
        UserOrderTargetExample example = makeExample(param);
        example.setOrderByClause("target_date");
        return userOrderTargetDao.selectByExample(example);
    }

    public List<TargetStatisticsDto> selectTargetStatistics(Date startTime, Date endTime, Integer offset, Integer limit){
        return userOrderTargetDao.selectTargetStatistics(startTime, endTime, offset, limit);
    }


    public UserOrderTargetExample makeExample(TargetParam param){
        UserOrderTargetExample example = new UserOrderTargetExample();
        UserOrderTargetExample.Criteria criteria = example.or();
        if(param.getUserId() != null){
            criteria.andUserIdEqualTo(param.getUserId());
        }
        if(param.getDate() != null){
            criteria.andTargetDateEqualTo(param.getDate());
        }
        if(param.getStartDate() != null && param.getEndDate() != null){
            criteria.andTargetDateBetween(param.getStartDate(), param.getEndDate());
        }
        if(param.getType() != null){
            criteria.andDateTypeEqualTo(param.getType());
        }
        return example;
    }

    @Data
    public static class TargetParam{

        private Integer userId;

        private Date date;

        private Date startDate;

        private Date endDate;

        private Integer type;
    }
}
